package cn.xbtop.blog.controller;

import cn.xbtop.blog.pojo.User;
import cn.xbtop.blog.service.UserService;
import cn.xbtop.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/6/3 16:19
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 全查询
     *
     * @param user
     * @return
     */
    @RequestMapping("/findAllUser")
    public Result<Object> findAllUser(User user) {
        List<User> allUser = userService.findAllUser(user);
        return new Result<>(allUser);

    }

    /**
     * 添加
     *
     * @param user
     * @return
     */
    @RequestMapping("/save")
    public Result<Object> save(@RequestBody User user) {
        userService.insert(user);
        return new Result<>("添加成功");
    }

    /**
     * 修改
     *
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public Result<Object> update(@RequestBody User user) {
        userService.updateByPrimaryKey(user);
        return new Result<>("修改成功");
    }


    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable Integer id) {
        userService.deleteByPrimaryKey(id);
        return new Result<>("删除成功");
    }


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById/{id}")
    public Result<Object> findById(@PathVariable Integer id) {
        User user = userService.selectByPrimaryKey(id);
        return new Result<>(user);
    }
}
